package com.example.androidproject.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.androidproject.R;
import com.example.androidproject.movie.activity.MovieDetailActivity;
import com.example.androidproject.movie.bean.ResultBeanData;
import com.example.androidproject.novel.bean.NovelBean;

import java.util.Collection;
import java.util.List;

public class TestAdapter extends RecyclerView.Adapter {
    private final Context mContext;
    private final LayoutInflater layoutInflater;
    private List<ResultBeanData.DataBean> movieList;
    private final int classifyFlag;
    private List<NovelBean.DataBean> novellist;
    String TAG = "GGSTUDY";

    public TestAdapter(Context applicationContext, Object list, int classifyFlag) {
        this.mContext = applicationContext;
        this.classifyFlag = classifyFlag;
        if (classifyFlag == 1) {
            this.movieList = (List<ResultBeanData.DataBean>) list;
        }
        if (classifyFlag == 2) {
            this.novellist = (List<NovelBean.DataBean>) list;
        }
        layoutInflater = LayoutInflater.from(mContext);
        Log.d(TAG, "TestAdapter: " + this.movieList);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void refresh(Object list) {
        if (classifyFlag == 1) {
            movieList.addAll((Collection<? extends ResultBeanData.DataBean>) list);
            notifyItemRangeChanged(0, movieList.size());
            Log.d(TAG, "refresh: " + list);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (classifyFlag == 1) {
            return new ContentViewHolder(layoutInflater.inflate(R.layout.classify3_item, parent, false));
        }
        if (classifyFlag == 2) {
            return new ContentViewHolder(layoutInflater.inflate(R.layout.classify3_novel_item, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ContentViewHolder) {
            ContentViewHolder contentViewHolder = (ContentViewHolder) holder;
            contentViewHolder.setData(position);
        }
    }

    @Override
    public int getItemCount() {
        if (classifyFlag == 1)
            return movieList.size();
        if (classifyFlag == 2) return novellist.size();
        return 0;
    }

    class ContentViewHolder extends RecyclerView.ViewHolder {
        public final ImageView iv_classify_content;
        public final TextView tv_classify_content, tv_novel_title, tv_novel_introduce, tv_novel_author, tv_novel_num;
        public final CardView gard_novel_body;
        public final LinearLayout ll_body_item;

        public ContentViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_classify_content = itemView.findViewById(R.id.iv_classify_content);
            tv_classify_content = itemView.findViewById(R.id.tv_classify_content);
            tv_novel_title = itemView.findViewById(R.id.tv_novel_title);
            tv_novel_introduce = itemView.findViewById(R.id.tv_novel_introduce);
            tv_novel_author = itemView.findViewById(R.id.tv_novel_author);
            tv_novel_num = itemView.findViewById(R.id.tv_novel_num);
            gard_novel_body = itemView.findViewById(R.id.gard_novel_body);
            ll_body_item = itemView.findViewById(R.id.ll_body_item);
        }

        public void setData(int position) {
            if (classifyFlag == 1) {
                Log.d(TAG, "setData: 123" + position);
                Glide.with(mContext).load(movieList.get(position).getSrc()).into(iv_classify_content);
                tv_classify_content.setText(movieList.get(position).getName());

                ll_body_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, MovieDetailActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putParcelable("data", movieList.get(position));
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);
                    }
                });
            }
            if (classifyFlag == 2) {
                Glide.with(mContext).load(novellist.get(position).getImage()).into(iv_classify_content);
                tv_novel_title.setText(novellist.get(position).getTitle());
                tv_novel_introduce.setText(novellist.get(position).getInfo());
                tv_novel_author.setText(novellist.get(position).getAuthor());
                tv_novel_num.setText(novellist.get(position).getNumber());

                gard_novel_body.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(mContext, "点击了" + position, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    }
}
